/**
 * 2017年4月25日
 */
package dao.wechatlab.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Repository;

import dao.BaseDao;
import dao.wechatlab.SubmittedTaskDao;
import pojo.weichatlab.SubmittedTask;

/**
 * @author Alex
 *
 */
@Repository
public class SubmittedTaskDaoImpl extends BaseDao implements SubmittedTaskDao {

	private static final String TABLE_NAME = "submitted_task";
	@Override
	public void insertTask(SubmittedTask st) {
		this.getHibernateTemplate().persist(st);
	}

	@Override
	public List<SubmittedTask> seletByUserId(String userid) {
		DetachedCriteria dc = DetachedCriteria.forClass(SubmittedTask.class);
		Restrictions.eq("userid", Integer.parseInt(userid));
		return (List<SubmittedTask>) this.getHibernateTemplate().findByCriteria(dc);
		
	}

	@Override
	public List<SubmittedTask> seletByType(final int type) {
		return this.jdbcTemplate.query(String.format("select * from %s where type=%d",TABLE_NAME,type), new RowMapper<SubmittedTask>(){

			@Override
			public SubmittedTask mapRow(ResultSet rs, int rowNum) throws SQLException {
				SubmittedTask st = new SubmittedTask();
				st.setId(rs.getInt("id"));
				st.setUserid(rs.getString("userid"));
				st.setType(rs.getInt("type"));
				st.setLast_update(rs.getDate("last_update"));
				st.setTime(rs.getDate("time"));
				st.setState(rs.getInt("state"));
				return st;
			}});
	}


	@Override
	public List<SubmittedTask> seletByState(final int state) {
		return this.jdbcTemplate.query(String.format("select * from %s where state=%d",TABLE_NAME,state), new RowMapper<SubmittedTask>(){

			@Override
			public SubmittedTask mapRow(ResultSet rs, int rowNum) throws SQLException {
				SubmittedTask st = new SubmittedTask();
				st.setId(rs.getInt("id"));
				st.setUserid(rs.getString("userid"));
				st.setType(rs.getInt("type"));
				st.setLast_update(rs.getDate("last_update"));
				st.setTime(rs.getDate("time"));
				st.setState(rs.getInt("state"));
				return st;
			}});
		
	}


	@Override
	public void updateTaskSucceed(SubmittedTask st) {
		st.setState(2);
		this.getHibernateTemplate().update(st);
	}

	@Override
	public void updateTaskFailed(SubmittedTask st) {
		st.setState(3);
		this.getHibernateTemplate().update(st);
	}

	@Override
	public void updateTaskRunning(SubmittedTask st) {
		st.setState(1);
		this.getHibernateTemplate().update(st);
	}

	@Override
	public void updateTaskSucceed(int id) {
	}

	@Override
	public void updateTaskFailed(int id) {
	}


	@Override
	public void updateTaskRunning(int id) {
	}

}
